package com.example.springboot_carlos.Controller;

import com.example.springboot_carlos.Util.DBUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

@RestController
public class DataBaseController {
    @RequestMapping("/createTable")
    public void createTable(HttpServletRequest request, HttpServletResponse response){
        int num= Integer.parseInt(request.getParameter("num"));
        try (Connection connection= DBUtil.getConnection();){
            updateTable(connection,num);
            showTable(response,request);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }


    @RequestMapping("/recreateTable")
    public void recreate(HttpServletResponse response){
        try {
            Connection connection = DBUtil.getConnection();
            Statement statement = connection.createStatement();
            String sql="DELETE FROM test ";
            statement.execute(sql);
            sql="DROP TABLE test";
            statement.execute(sql);
            sql= "CREATE TABLE test(" +
                 "id INT PRIMARY KEY AUTO_INCREMENT," +
                 "username VARCHAR(30)," +
                 "password VARCHAR(30)," +
                 "nickname VARCHAR(30)," +
                 "age INT(3))";
            statement.execute(sql);
            response.sendRedirect("/create_table.html");
        } catch (SQLException | IOException throwables) {
            throwables.printStackTrace();
        }
    }

    private static void updateTable(Connection connection,int num) throws SQLException {
        String[] username=randomUsername(num);
        String[] nickname=randomNickname(num);
        String[] password = randomPassword(num);
        int[] age = randomAge(num);
        String sql="INSERT INTO test (username,password,nickname,age)  " +
                   "VALUES(?,?,?,?)";
        PreparedStatement ps = connection.prepareStatement(sql);
        for(int i=0;i<num;i++){
            ps.setString(1,username[i]);
            ps.setString(2,password[i]);
            ps.setString(3,nickname[i]);
            ps.setInt(4,age[i]);
            ps.executeUpdate();
        }
    }

    private  void showTable(HttpServletResponse response,HttpServletRequest request){
        try (Connection connection = DBUtil.getConnection();){
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>用户列表</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>用户列表</h1>");
            pw.println("<table border=\"1\">");
            pw.println("<tr>");
            pw.println("<td>ID</td>");
            pw.println("<td>用户名</td>");
            pw.println("<td>密码</td>");
            pw.println("<td>昵称</td>");
            pw.println("<td>年龄</td>");
            pw.println("</tr>");

            String sql = "SELECT id,username,password,nickname,age FROM test " +
                         "LIMIT  ?,10";
            int id = Integer.parseInt(request.getParameter("id"));
            int num = Integer.parseInt(request.getParameter("num"));
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1,id-1);
            ResultSet rs = ps.executeQuery();
            if(id>num){
                response.sendRedirect("/have_no_data_fail.html");
                return;
            }
            while (rs.next()){
                pw.println("<tr>");
                pw.println("<td>"+rs.getInt("id")+"</td>");
                pw.println("<td>"+rs.getString("username")+"</td>");
                pw.println("<td>"+rs.getString("password")+"</td>");
                pw.println("<td>"+rs.getString("nickname")+"</td>");
                pw.println("<td>"+rs.getInt("age")+"</td>");
                pw.println("</tr>");
                id++;
            }
            pw.println("</table>");
            pw.println("<h5><a href='/createTable/?id="+id+"&num="+num+"'>下一页</a></h5>");
            pw.println("<h2><a href=\"/recreateTable\">重新生成</a></h2>");
            pw.println("<h2><a href=\"/index.html\">返回首页</a></h2>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");


        } catch (IOException | SQLException e) {
            e.printStackTrace();
        }

    }

    static String[] randomUsername(int num){
        String[] username = new String[num];
        for(int i=0;i<num;i++){
            int nameLength=(int)(Math.random()*5+3);
            char[] name= new char[nameLength];
            name[0]=(char) (Math.random()*26+65);
            for(int j =1;j<name.length;j++){
                name[j]=(char)(Math.random()*26+97);
            }
            username[i]=new String(name);
        }
        return username;
    }

    static String[] randomNickname(int num){

        String [] nickname =new String[num];
        for(int i=0;i<num;i++){
            int nicknameLength =(int)(Math.random()*8+3);
            char[]nicknameChar= new char[nicknameLength];
            nicknameChar[0]=(char) (Math.random()*26+65);
            for(int j =1;j<nicknameLength;j++){
                nicknameChar[j]=(char)(Math.random()*26+97);
            }
            nickname[i]=new String(nicknameChar);
        }
        return nickname;
    }

    private static String[] randomPassword(int num){
        String[] password = new String[num];
        for(int i=0;i<num;i++){
            int  pw=(int)(Math.random()*900000+100000);
            password[i]= String.valueOf(pw);
        }
        return password;
    }

    static String[] randomContent(int num){
        String [] content =new String[num];
        for(int i=0;i<num;i++){
            int contentLength =(int)(Math.random()*20+10);
            char[]contentChar= new char[contentLength];
            contentChar[0]=(char) (Math.random()*26+65);
            for(int j =1;j<contentLength;j++){
                contentChar[j]=(char)(Math.random()*26+97);
            }
            content[i]=new String(contentChar);
        }
        return content;
    }

    private static int[] randomAge(int num){
        int[]age= new int[num];
        for(int i=0;i<num;i++){
            age[i]=(int)(Math.random()*99+1);
        }
        return age;
    }


}
